package com.lzyyj.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** 
 * 分页对象. 包含当前页数据及分页信息如总记录数. 
 * 
 * @author springside 
 *  
 */ 
@SuppressWarnings("serial") 
public class Page<T> implements Serializable { 
	
    private static long DEFAULT_PAGE_SIZE = 20; 

    private long pageSize = DEFAULT_PAGE_SIZE; // 每页的记录数 

    private long startIndex; // 当前页第一条数据在List中的位置,从0开始 
    
    private T findByExample;
    
    private Map<String, Object> condition = new HashMap<String, Object>();

    private List<T> data; // 当前页中存放的记录,类型一般为List 

    private long totalCount; // 总记录数 
    
    private String pageShowTemplate;
    
    private String orderFields;	// 排序字段列表，以逗号分隔

    /** 
     * 默认构造方法，只构造空页. 
     */ 
    public Page() { 
        this(0, -1l, DEFAULT_PAGE_SIZE, new ArrayList<T>()); 
    } 

    /** 
     * 构造方法. 
     * 
     * @param pageSize  本页容量 
     */ 
    public Page(long pageSize) { 
        this.pageSize = pageSize; 
    } 

    /** 
     * 构造方法. 
     * 
     * @param pageNo  页号 
     * @param pageSize  本页容量 
     */ 
    public Page(long pageNo, long pageSize) { 
        this.pageSize = pageSize; 
        this.setPageNo(pageNo);
    } 

    /** 
     * 构造方法. 
     * 
     * @param start  本页数据在数据库中的起始位置 
     * @param totalSize 数据库中总记录条数 
     * @param pageSize  本页容量 
     * @param data    本页包含的数据 
     */ 
    public Page(long start, Long totalSize, long pageSize, List<T> data) { 
        this.pageSize = pageSize; 
        this.startIndex = start; 
        this.totalCount = totalSize; 
        this.data = data; 
    } 

    /** 
     * 取总记录数. 
     */ 
    public long getTotalCount() { 
        return this.totalCount; 
    } 

    /** 
     * 取总记录数. 
     */ 
    public void setTotalCount(long totalCount) { 
        this.totalCount = totalCount; 
    } 

    /** 
     * 取总页数. 
     */ 
    public long getTotalPageCount() {
    	long totalPageCount = 0;
        if (totalCount > 0 && pageSize > 0){
			totalPageCount = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
		}
        return totalPageCount;
    } 

    /** 
     * 取每页数据容量. 
     */ 
    public long getPageSize() { 
        return pageSize; 
    } 

    /** 
     * 取当前页中的记录. 
     */ 
    public List<T> getData() { 
        return data; 
    } 

    /** 
     * 该页是否有下一页. 
     */ 
    public boolean isHasNextPage() { 
        return getPageNo() < this.getTotalPageCount(); 
    } 

	public long getNextPage() {
		return getPageNo()+1;
	}

    /** 
     * 该页是否有上一页. 
     */ 
    public boolean isHasPreviousPage() { 
        return getPageNo() > 1; 
    } 

	public long getPreviousPage() {
		if (isHasPreviousPage()){
			return getPageNo()-1;
		}else{
			return getPageNo();
		}
	} 

	public String getPageShowTemplate() {
		pageShowTemplate="<script>function page(n){document.getElementById('page.pageNo').value=n;document.forms[0].submit();}</script>";
		pageShowTemplate+="共<span style=\"color:red;\">"+getTotalCount()+"</span>条记录,每页<input name=\"page.pageSize\" value="+getPageSize()+" size=\"3\" />条数据,共<span style=\"color:red;\">"+getTotalPageCount()+"</span>页,当前显示第<input id=\"page.pageNo\" name=\"page.pageNo\" value="+getPageNo()+" size=\"3\" onchange=\"page(this.value);\" />页";
		//pageShowTemplate+="找到<span style=\"color:black;\">"+getTotalCount()+"</span>条记录,共<span style=\"color:black;\">"+getTotalPageCount()+"</span>页,当前显示第<input id=\"page.pageNo\" name=\"page.pageNo\" value="+getPageNo()+" size=\"1\" onchange=\"page(this.value);\" />页";
		if (getPageNo()>1) {
			pageShowTemplate+="&nbsp;&nbsp;<a href=\"javascript:page(1)\">首页</a>&nbsp;&nbsp;<a href=\"javascript:page("+(getPageNo()-1)+")\">上一页</a>";
		}
		else{
			pageShowTemplate+="&nbsp;&nbsp;<a disabled=\"true\">首页</a>&nbsp;&nbsp;<a disabled=\"true\">上一页</a>";
		}
		
		if(getPageNo()<getTotalPageCount()){
			pageShowTemplate+="&nbsp;&nbsp;<a href=\"javascript:page("+(getPageNo()+1)+")\">下一页</a>&nbsp;&nbsp;<a href=\"javascript:page("+getTotalPageCount()+")\">末页</a>";
		}else{
			pageShowTemplate+="&nbsp;&nbsp;<a disabled=\"true\">下一页</a>&nbsp;&nbsp;<a disabled=\"true\">末页</a>";
		}
		return pageShowTemplate;
	}

	public void setPageShowTemplate(String pageShowTemplate) {
		this.pageShowTemplate = pageShowTemplate;
	}

	public long getPageNo() {
		return (pageSize+startIndex)/pageSize;
	}

	public void setPageNo(long pageNo) {
		startIndex = startIndex + (pageNo-1)*pageSize-(getPageNo()-1)*pageSize;
	}

	public T getFindByExample() {
		return findByExample;
	}

	public void setFindByExample(T findByExample) {
		this.findByExample = findByExample;
	}

	public Map<String, Object> getCondition() {
		return condition;
	}

	public void setCondition(Map<String, Object> condition) {
		this.condition = condition;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public void setPageSize(long pageSize) {
		long pageNo = getPageNo();
		long offset = getOffset();
		startIndex = 0;
		this.pageSize = pageSize;
		startIndex = offset;
		setPageNo(pageNo);
	}

	public long getOffset() {
		return startIndex - (getPageNo()-1)*pageSize;
	}

	public String getOrderFields() {
		return orderFields;
	}

	public void setOrderFields(String orderFields) {
		this.orderFields = orderFields;
	}

	public long getStartIndex() {
		return startIndex;
	} 

	public void setStartIndex(long startIndex) {
		this.startIndex = startIndex;
	}
}

